package com.example.demo.mapper;

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    @Select("select * from user_info")
     List<UserInfo> selectAll();


    @Select("select username, password, age, gender, phone from user_info where id= 4 ")
     List<UserInfo> selectById();

    //传递参数(当只有一个参数的时候，#{}里面的名称可以和参数名不一样，但有多个参数时则不行)
    @Select(("select username, `password`, age, gender, phone from user_info where id= #{id}"))
     List<UserInfo> selectById1(Integer id);

    @Select(("select username, `password`, age, gender, phone from user_info where id= #{aa}"))
    List<UserInfo> selectById2(Integer id);

//   通过 @Param,设置参数的别名,如果使⽤ @Param 设置别名, #{...}⾥⾯的属性名必须和@Param 设置的⼀样
    @Select(("select username, `password`, age, gender, phone from user_info where id= #{userid}"))
    List<UserInfo> selectById3(@Param("userid") Integer id);

    @Select("SELECT * FROM `user_info` where username=#{userName} and `password` = #{password} ")
    List<UserInfo> selectByNameAndPassword(@Param("password") String password, @Param("userName") String username);

//   SQl语句太长使用"+"进行拼接
    @Insert("insert into user_info (username, `password`, age,gender,phone) " +
            "values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);

//   如果设置了 @Param 属性, #{...} 需要使⽤ 参数.属性 来获取
    @Insert("insert into user_info (username, `password`, age, gender, phone) "+
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age}," +
            "#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

//    如果想要拿到⾃增id, 需要在Mapper接⼝的⽅法上添加⼀个Options的注解
//    返回类型可以为void
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age,gender,phone)" +
            "values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert3(UserInfo userInfo);


    //解决一些属性没有赋值的问题
    //方法一：起别名
    @Select("select id, username, `password`, age, gender, phone, delete_flag as deleteFlag, " +
            "create_time as createTime, update_time as updateTime from user_info")
    List<UserInfo> selectAll2();

    //方法二：结果映射
    @Select("select * from user_info")
    @Results({
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> selectAll3();

//  如果其他SQL, 也希望可以复⽤这个映射关系, 可以给这个Results定义⼀个名称
    @Select("select * from user_info")
    @Results(id = "resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> selectAll4();

    @Select("select * from user_info")
    @ResultMap(value = "resultMap")
    List<UserInfo> selectAll5();

    @Select("select id, username, `password`, age, gender, phone, delete_flag, " +
            "create_time, update_time from user_info")
    public List<UserInfo> queryAllUser();



}
